package com.xx.pmis.web.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Shop;
import com.xx.pmis.services.ShopService;
import com.xx.pmis.web.forms.ShopForm;
import com.xx.pmis.web.util.Rand;
import com.xx.pmis.web.util.ShopUtil;

/**
 * @class 商店Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class ShopAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(ShopAction.class);

	ShopService shopService = getPMISServices().getShopService();

	ShopUtil shopUtil = new ShopUtil();

	// 查询所有商店
	public ActionForward listShop(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<Shop> all = null;
		try {
			all = shopService.getAllShop();
		} catch (Exception e) {
			log.info("查询商店异常！" + e.toString());
		}
		List<Shop> list = shopUtil.getAllShopIncludeExt(all);
		request.setAttribute("shopList", list);
		request.setAttribute("position", "物资管理/商店");
		request.setAttribute("error", "不顾一切成为网购达人！");
		return mapping.findForward("listShop");
	}

	// 新增
	public ActionForward addShop(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("position", "商店/新增");
		request.setAttribute("shopCode", "1000" + Rand.getRand());
		return mapping.findForward("addShop");
	}

	// 新增，处理方法
	public ActionForward addShop_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		ShopForm f = (ShopForm) form;
		String shopName = f.getShopName(); // 商店名称
		String shopCode = f.getShopCode(); // 商店编码
		String shopAddress = f.getShopAddress(); // 地址
		String shopDesc = f.getShopDesc(); // 描述

		Shop shop = new Shop();
		shop.setShopName(shopName);
		shop.setShopCode(shopCode);
		shop.setShopAddress(shopAddress);
		shop.setShopDesc(shopDesc);
		shop.setShopType("1");

		try {
			shopService.saveOrUpdateShop(shop);
		} catch (Exception e) {
			log.info("保存商店异常！" + e.toString());
		}

		listShopByPage(mapping, form, request, response);
		return mapping.findForward("listShop");
	}

	// 编辑
	public ActionForward editShop(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String shopID = request.getParameter("shopID");
		String pageNum = request.getParameter("pageNum");

		Shop shop = new Shop();
		try {
			shop = shopService.getShopByShopID(shopID);
		} catch (Exception e) {
			log.info("跟进商店ID查询该商店异常！" + e.toString());
		}

		request.setAttribute("pageNum", pageNum);
		request.setAttribute("shop", shop);
		request.setAttribute("position", "商店/编辑");
		return mapping.findForward("editShop");
	}

	// 编辑，处理方法
	public ActionForward editShop_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		ShopForm f = (ShopForm) form;
		String shopID = f.getShopID();
		String shopName = f.getShopName(); // 商店名称
		// String shopCode = f.getShopCode(); // 商店编码
		String shopAddress = f.getShopAddress(); // 地址
		String shopDesc = f.getShopDesc(); // 描述

		Shop shop = null;
		try {
			shop = shopService.getShopByShopID(shopID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		shop.setShopName(shopName);
		// shop.setShopCode(shopCode);
		shop.setShopAddress(shopAddress);
		shop.setShopDesc(shopDesc);
		try {
			shopService.saveOrUpdateShop(shop);
		} catch (Exception e) {
			log.info("保存或更新【商店】异常！" + e.toString());
		}

		request.setAttribute("pageNum", pageNum);
		listShopByPage(mapping, form, request, response);
		return mapping.findForward("listShop");
	}

	// 删除
	public ActionForward delShop(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		Shop shop = null;
		ShopForm f = (ShopForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				shop = shopService.getShopByShopID(ids[i]);
			} catch (Exception e) {
				log.info("根据【商店ID】查询该【商店】异常！" + e.toString());
			}
			try {
				shopService.deleteShop(shop);
			} catch (Exception e) {
				log.info("保存或更新【商店】异常！" + e.toString());
			}
		}

		request.setAttribute("pageNum", pageNum);
		listShopByPage(mapping, form, request, response);
		return mapping.findForward("listShop");
	}

	// 更改礼品状态：已读、未读；在藏、丢失
	public ActionForward stateShop(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String shopID = request.getParameter("shopID");
		String state = request.getParameter("state");
		String pageNum = request.getParameter("pageNum");
		Shop shop = new Shop();
		try {
			shop = shopService.getShopByShopID(shopID);
		} catch (Exception e) {
			String errorPage = "【物品管理/商店/状态】根据【商店ID】查询该【商店】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		if ("yijihuo".equals(state)) {// 已激活
			shop.setShopType("0");
		} else if ("weijihuo".equals(state)) {// 未激活
			shop.setShopType("1");
		} else {
			log.error("输入参数应为“yijihuo”，“weijihuo”。");
		}

		try {
			shopService.saveOrUpdateShop(shop);
		} catch (Exception e) {
			String errorPage = "【物品管理/商店/状态】保存或更新【商店】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listShopByPage(mapping, form, request, response);
		return mapping.findForward("listShop");
	}

	// 分页显示
	public ActionForward listShopByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = shopService.getCountOfShop();
		} catch (Exception e) {
			String errorPage = "【物资管理/商店】查询【商店】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}

		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<Shop> all = null;
		try {
			all = shopService.getAllShopByPage(Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【物资管理/商店】查询【商店】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		List<Shop> list = shopUtil.getAllShopIncludeExt(all);
		// 排序 根据消费次数排序
		// List<Shop> list =
		// shopUtil.getAllShopIncludeExtAfterOrdination(shopUtil.getAllShopIncludeExt(all));
		request.setAttribute("shopList", list);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "shop.do");
		request.setAttribute("position", "物资管理/商店");
		return mapping.findForward("listShop");
	}

}